<?php

namespace Xin\LaravelFortify\Listener;

use Illuminate\Support\Facades\Log;

trait SafetyHandling
{
	/**
	 * @var mixed
	 */
	protected $event;

	/**
	 * Handle the event.
	 *
	 * @param mixed $event
	 * @return void
	 */
	public function handle($event): void
	{
		try {
			$this->event = $event;
			$this->action($event);
		} catch (\Throwable $e) {
			$this->catch($e);
			Log::error(static::class . " error", [$event, $e]);
		}
	}

	/**
	 * @param mixed $event
	 * @return mixed
	 */
	abstract protected function action(mixed $event);

	/**
	 * 异常事件
	 * @param \Throwable $e
	 * @return void
	 */
	protected function catch(\Throwable $e)
	{
	}
}
